package com.beep.erp.common.base;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;


import com.beep.erp.R;
import com.beep.erp.common.widget.StatusBarCompat;

import butterknife.BindView;

/**
 * FileName: ToolbarActivity
 * description:
 * Date: 2018/9/12
 */
public abstract class ToolbarActivity<P extends BasePresenter, M> extends BaseActivity<P, M> implements Toolbar.OnMenuItemClickListener{

    @BindView(R.id.appbar_layout)
    AppBarLayout mAppBar;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_titleLeft)
    TextView tvTitleLeft;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.sp_rank)
    AppCompatSpinner spRank;


    protected boolean isBack() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.initToolBar(); //初始化ToolBar
//        StatusBarCompat.compat(this);   //适配4.4系统状态栏
//        StatusBarCompat.compat(this);  //设置全局状态栏颜色
    }


    private void initToolBar() {
        if (toolbar == null || mAppBar == null) {
            throw new IllegalStateException(
                    "The subclass of ToolbarActivity must contain a toolbar.");
        }
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);//让原始的toolbar的title不显示
            if (isBack()) {
               // actionBar.setDisplayHomeAsUpEnabled(true);
                toolbar.setNavigationIcon(R.mipmap.ic_menu_back);
//                toolbar.setContentInsetStartWithNavigation(0);
            }
        }
        toolbar.setOnMenuItemClickListener(this);
        if (Build.VERSION.SDK_INT >= 21) {
            toolbar.setElevation(10.6f);
        }
    }

//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        unbinder.unbind();
//    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }


    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return true;
    }

    protected TextView getTvTitle() {
        return tvTitle;
    }

    protected Toolbar getToolbar() {
        return toolbar;
    }

    protected EditText getEtSearch() {
        return etSearch;
    }

    protected AppCompatSpinner getSpRank() {
        return spRank;
    }


    /**
     * 隐藏软键盘
     */
    public void hideSoftKeyboard() {
        ((InputMethodManager) getSystemService(
                Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                etSearch.getWindowToken(), 0);
    }

    /**
     * 显示软键盘
     */
    public void showSoftKeyboard() {
        etSearch.requestFocus();
        ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
                .showSoftInput(etSearch, InputMethodManager.SHOW_FORCED);
    }

}
